package cn.zswltech.gruul.xxljob;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.hikvision.aksk.entity.InterfaceData;
import com.hikvision.aksk.utils.GetAuthorizationUtil;
import com.hikvision.aksk.utils.SendHttpRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jim
 * @version 1.0.0
 * @descripition: 访问云狐工具类
 * @date 2024/2/19 10:10
 */
public class YunHuConnectUtil {

    /**
     * 生成访问用载体
     * @param url 访问接口
     * @param param 带参
     * @return 访问用载体
     */
    public static InterfaceData buildInterfaceData(String url,String param){
        InterfaceData interfaceData = new InterfaceData();
        interfaceData.setProjectKey(YunHuConstant.PROJECT_CODE);
        interfaceData.setHttpType("http");
        interfaceData.setContentType("application/json");
        interfaceData.setUrl(url);
        interfaceData.setParam(param);
        interfaceData.setAccessKey(YunHuConstant.ACCESS_KEY);
        interfaceData.setDeveloperSk(YunHuConstant.SECRET_KEY);
        interfaceData.setDate(String.valueOf(System.currentTimeMillis()));
        interfaceData.setMethodType("POST");
        return interfaceData;
    }

    /**
     * 请求云狐接口
     * @param interfaceData 请求载体
     * @return 返回结果
     */
    public static String request(InterfaceData interfaceData) throws Exception {
        JSONObject paramMap = new JSONObject();
        String paramArray = "";
        String param = interfaceData.getParam();
        if (param.startsWith("[") && param.endsWith("]")) {
            paramArray = param;
        } else {
            paramMap = JSONObject.parseObject(param, new Feature[]{Feature.OrderedField});
        }

        GetAuthorizationUtil getAuthorizationUtil = new GetAuthorizationUtil();
        Map<String, String> map = new HashMap();
        String uri = "";
        String url = interfaceData.getUrl();

        for(int i = 0; i < 3; ++i) {
            url = url.substring(url.indexOf("/") + 1);
        }

        uri = "/" + url;
        String httpType = interfaceData.getHttpType();
        if ("".equals(httpType) || null == httpType) {
            httpType = "http";
        }

        String authorization = getAuthorizationUtil.getAuthorization(interfaceData.getAccessKey(), interfaceData.getDeveloperSk(), interfaceData.getMethodType(), interfaceData.getContentType(), uri, interfaceData.getDate(), paramMap, map, paramArray);
        SendHttpRequest sendHttpRequest = new SendHttpRequest();
        String methodType = interfaceData.getMethodType().toLowerCase();
        String date = (String)map.get("date");
        String result = "";
        if ("get".equals(methodType)) {
            result = sendHttpRequest.getMethod(interfaceData.getAppKey(), interfaceData.getProjectKey(), httpType, interfaceData.getContentType(), authorization, interfaceData.getUrl(), paramMap, date);
        } else {
            result = sendHttpRequest.postMethod(interfaceData.getAppKey(), interfaceData.getProjectKey(), httpType, interfaceData.getContentType(), authorization, interfaceData.getUrl(), paramMap, date, paramArray);
        }
        return result;
    }
}
